package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.Banner;
import com.clkj.xiuzheng.pojo.shangcheng.Health;
import com.clkj.xiuzheng.pojo.shangcheng.HealthUser;
import com.clkj.xiuzheng.pojo.shangcheng.Notice;
import com.clkj.xiuzheng.pojo.shangcheng.vo.HealthUserVo;
import com.clkj.xiuzheng.pojo.shangcheng.vo.HealthVo;
import com.clkj.xiuzheng.pojo.shangcheng.vo.HomeVo;
import com.clkj.xiuzheng.service.shangcheng.SystemService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/scSystem")
@CrossOrigin
@Api(value = "/scSystem", tags = {"Z商城系统管理"})
public class SystemController {

    @Autowired
    private SystemService systemService;

    @ApiOperation(value = "添加系统公告")
    @PostMapping("/addNotice")
    public Result addNotice(Notice notice) {
        try {
            systemService.addNotice(notice);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "获取所有系统公告")
    @PostMapping("/listNotice")
    public Result listNotice(Integer page ,Integer size,Integer companyId,Integer flag) {
        try {
            PageInfo notices = systemService.listNotice(page,size,companyId,flag);
            return new Result(true, StatusCode.OK, "查询成功",notices);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    @ApiOperation(value = "修改公告的发布状态")
    @PostMapping("/releaseNotice")
    public Result releaseNotice(Integer id,Integer status,String dateTime) {
        try {
            systemService.releaseNotice(id,status,dateTime);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }
    @ApiOperation(value = "审核系统公告")
    @PostMapping("/reviewNotice")
    public Result reviewNotice(Integer noticeId,Integer companyId) {
        try {
            systemService.reviewNotice(noticeId,companyId);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }

    //    @ApiOperation(value = "用户获取系统公告")
//    @PostMapping("/getAnnounce")
//    public Result getAnnounceByType(@ApiParam(name = "type", value = "公告面向用户类型")@RequestParam Integer type) {
//        try {
//            systemService.getAnnounce(type);
//            return new Result(true, StatusCode.OK, "添加成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }
//    @ApiOperation(value = "公司获取系统公告")
//    @PostMapping("/getAnnounceByCompantId")
//    public Result getAnnounceByCompantId(@ApiParam(name = "type", value = "公告面向用户类型")@RequestParam Integer type) {
//        try {
//            systemService.getAnnounceByCompantId(type);
//            return new Result(true, StatusCode.OK, "添加成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false, StatusCode.ERROR, "添加失败");
//        }
//    }
    @ApiOperation(value = "添加轮播图")
    @PostMapping("/addBanner")
    public Result addBanner(Banner banner,HttpServletRequest request){
        try {
            systemService.addBanner(banner,request);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
    @ApiOperation(value = "修改轮播图")
    @PostMapping("/updateBanner")
    public Result updateBanner(Banner banner,HttpServletRequest request){
        try {
            systemService.updateBanner(banner,request);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
    @ApiOperation(value = "小程序获取轮播图")
    @PostMapping("/getBanner")
    public Result getBanner(){
        try {
            List<Banner> list = systemService.getBanner();
            return new Result(true, StatusCode.OK, "添加成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
    @ApiOperation(value = "后台获取轮播图")
    @PostMapping("/backGetBanner")
    public Result backGetBanner(@RequestParam Integer pageSize,
                                @RequestParam Integer pageNum){
        try {
            PageInfo list = systemService.listBanner(pageSize,pageNum);
            return new Result(true, StatusCode.OK, "添加成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
    @ApiOperation(value = "根据轮播图绑定类型获取信息")
    @PostMapping("/getBandingByType")
    public Result getBandingByType(@ApiParam(name = "type", value = "轮播图绑定类型")@RequestParam Integer type,
                                   @ApiParam(name = "name", value = "绑定类型名称")@RequestParam String name){
        try {
           Result result = systemService.getBandingByType(type,name);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "上下架轮播图")
    @PostMapping("/updateStatus")
    public Result updateStatus(@ApiParam(name = "ids", value = "轮播图id")@RequestParam String ids,
                                   @ApiParam(name = "status", value = "更改状态")@RequestParam Integer status){
        try {
             systemService.updateStatus(ids,status);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "轮播图根据id获取商品或百科内容")
    @PostMapping("/getBandingById")
    public Result getBandingById(@ApiParam(name = "bandingId", value = "绑定Id")@RequestParam Integer bandingId,@ApiParam(name = "flag", value = "绑定类型0商品1文章2视频")@RequestParam Integer flag){
        try {
            Result result = systemService.getBandingById(bandingId,flag);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "主页获取数据")
    @PostMapping("/getHomeData")
    public Result getHomeData(Integer companyId){
        try {
            HomeVo homeVo = systemService.getHomeData(companyId);
            return new Result(true, StatusCode.OK, "查询成功",homeVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "主页获取订单每月数据")
    @PostMapping("/getHomeDataOrderMonth")
    public Result getHomeDataOrderMonth(Integer companyId){
        try {
            Map<Integer, Object> map = systemService.getHomeDataOrderMonth(companyId);
            return new Result(true, StatusCode.OK, "查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "主页获取用户每月数据")
    @PostMapping("/getHomeDataUserMonth")
    public Result getHomeDataUserMonth(Integer companyId){
        try {
            Map<Integer, Object> map = systemService.getHomeDataUserMonth(companyId);
            return new Result(true, StatusCode.OK, "查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }


    @ApiOperation(value = "后台健康档案问题汇总")
    @PostMapping("/addHealth")
    public Result addHealth(HealthVo healthVo){
        try {
            systemService.addHealth(healthVo);
            return new Result(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "根据厂商看商品销量")
    @PostMapping("/getSalesByFactoryId")
    public Result getSalesByFactoryId(){
        try {
            Map map = systemService.getSalesByFactoryId();
            return new Result(true, StatusCode.OK, "查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "后台健康档案问题汇总列表")
    @PostMapping("/listHealth")
    public Result listHealth(){
        try {
            List<Health> healths = systemService.listHealth();
            return new Result(true, StatusCode.OK, "查询成功",healths);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "后台健康档案问题汇总列表")
    @PostMapping("/deleteHealth")
    public Result deleteHealth(String ids) {
        try {
            systemService.deleteHealth(ids);
            return new Result(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "查询商品的销量信息")
    @PostMapping("/getSalesByProductId")
    public Result getSalesByProductId(@ApiParam(name = "productId", value = "商品id")@RequestParam Integer productId){
        try {
            Map map = systemService.getSalesByProductId(productId);
            return new Result(true, StatusCode.OK, "查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @ApiOperation(value = "小程序添提交问卷")
    @PostMapping("/addHealthUser")
    public Result addHealthUser(String healthIds,String answerIds,Integer userId){
        try {
            systemService.addHealthUser(healthIds,answerIds,userId);
            return new Result(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @ApiOperation(value = "小程序获取用户问卷调查")
    @PostMapping("/listHealthUser")
    public Result listHealthUser(Integer userId){
        try {
            List<Health> healthUsers = systemService.listHealthUser(userId);
            return new Result(true, StatusCode.OK, "查询成功",healthUsers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
}
